package com.fankui.serviceucenter.controller;


import com.fankui.commonutils.JwtUtils;
import com.fankui.commonutils.R;
import com.fankui.serviceucenter.entity.MemberVo;
import com.fankui.serviceucenter.entity.RegisterVo;
import com.fankui.serviceucenter.entity.UcenterMember;
import com.fankui.serviceucenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 范奎
 * @since 2023-12-06
 */
@Api(description = "前台用户登录和注册")
@RestController
@RequestMapping("/ucenterservice/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@ApiParam(name = "ucenterMember", value = "用户登录对象", required = true) @RequestBody UcenterMember ucenterMember) {
        String token = memberService.login(ucenterMember);
        return R.ok().data("token", token);
    }

    @ApiModelProperty(value = "会员注册")
    @PostMapping("register")
    public R register(@ApiParam(name = "registerVo", value = "用户注册对象", required = true) @RequestBody RegisterVo registerVo) {
        boolean flag = memberService.register(registerVo);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiModelProperty(value = "根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(@ApiParam(name = "request", value = "HttpServletRequest对象", required = true) HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("member", member);
    }

    @ApiOperation(value = "根据用户id获取用户信息")
    @GetMapping("getMemberVo/{memberId}")
    public R getMemberVo(@ApiParam(name = "memberId", value = "用户id", required = true) @PathVariable String memberId) {
        UcenterMember ucenterMember = memberService.getById(memberId);
        MemberVo memberVo = new MemberVo();
        BeanUtils.copyProperties(ucenterMember, memberVo);
        return R.ok().data("memberVo", memberVo);
    }

    @ApiOperation(value = "获取某一天的注册人数")
    @GetMapping("getRegisterCount/{day}")
    public R getRegisterCount(@ApiParam(value = "day", name = "日期", required = true) @PathVariable String day) {
        Integer count = memberService.getRegisterCountByDay(day);
        return R.ok().data("registerCount", count);
    }
}

